<template>
  <div>
    <el-form ref="addRoleformRef" :model="formData" :rules="rules" size="medium" label-width="100px" title-text="新增员工">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入姓名" clearable :style="{width: '100%'}" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" placeholder="角色描述手机" clearable :style="{width: '100%'}" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :style="{width: '100%'}"
          placeholder="请选择入职时间"
          clearable
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          placeholder="请选择聘用形式"
          clearable
          :style="{width: '100%'}"
        >
          <el-option
            v-for="(item, index) in hireType"
            :key="index"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" placeholder="请输入工号" clearable :style="{width: '100%'}" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" placeholder="请输入部门" clearable :style="{width: '100%'}" @focus="getDepartments" />
        <!-- 树 组件 -->
        <el-tree
          v-if="treeShow"
          v-loading="loading"
          :data="departs"
          :props="{label: 'name'}"
          default-expand-all
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :style="{width: '100%'}"
          placeholder="请选择转正时间"
          clearable
        />
      </el-form-item>
      <el-form-item size="large" align="center">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getDepartmentsAPI } from '@/api/departments'
import { tranListToTreeData } from '@/utils' // 扁平数据转化为树形
import employees from '@/api/constant/employees.js'
import { addEmployeeAPI } from '@/api/employee'
const { hireType } = employees
export default {
  components: {},
  props: [],
  data() {
    return {
      hireType,
      departs: [],
      treeShow: false,
      loading: false,
      formData: {
        username: undefined,
        mobile: undefined,
        timeOfEntry: null,
        formOfEmployment: undefined,
        workNumber: undefined,
        departmentName: undefined,
        correctionTime: null
      },
      rules: {
        username: [{
          required: true,
          message: '请输入姓名',
          trigger: 'blur'
        }],
        mobile: [{
          required: true,
          pattern: /^1[3-9]\d{9}$/,
          message: '输入正确手机号',
          trigger: 'blur'
        }],
        timeOfEntry: [{
          required: true,
          message: '请选择入职时间',
          trigger: 'change'
        }],
        formOfEmployment: [{
          required: true,
          message: '请选择聘用形式',
          trigger: 'change'
        }],
        workNumber: [{
          required: true,
          message: '请输入工号',
          trigger: 'blur'
        }],
        departmentName: [{
          required: true,
          message: '请输入部门',
          trigger: 'change'
        }],
        correctionTime: []
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async getDepartments() {
      this.treeShow = true
      this.loading = true
      const { depts } = await getDepartmentsAPI()
      this.departs = tranListToTreeData(depts, '') // 扁平数据转化为树形
      this.loading = false
    },
    submitForm() {
      this.$refs['addRoleformRef'].validate(async valid => {
        if (!valid) return
        // TODO 提交表单
        await addEmployeeAPI(this.formData)
        this.$message.success('操作成功')
        this.$emit('getEmployeeList')
        this.$emit('addPopHandleClose')
      })
    },
    resetForm() {
      // 重置数据
      console.log(this)
      this.formData = this.$options.data().formData
      // 重置校验
      this.$refs['addRoleformRef'].resetFields()
      this.$emit('addPopHandleClose')
    },
    selectNode({ name }) {
      this.formData.departmentName = name
      this.treeShow = false
    }
  }
}

</script>
<style>
</style>
